package cn.itcast.itcaststore.web.servlet.client;

import java.io.IOException;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import cn.itcast.itcaststore.domain.PageBean;
import cn.itcast.itcaststore.service.ProductService;

public class ShowProductByPageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1.定义当前页码，默认为1
		int currentPage = 1;
		String _currentPage = request.getParameter("currentPage");
		if (_currentPage != null) {
			currentPage = Integer.parseInt(_currentPage);
		}
		// 2.定义每页显示条数,默认为4
		int currentCount = 4;
		String _currentCount = request.getParameter("currentCount");
		if (_currentCount != null) {
			currentCount = Integer.parseInt(_currentCount);
		}
		// 3.获取查找的分类
		String category = "全部商品";
		String _category = request.getParameter("category");
//		System.out.println("== ShowProductByPageServlet =1== _category " + _category);
		if (_category != null) {
			_category=changechar(_category);
			_category=unicodeToCn(_category);
			category = _category;
		}
//		System.out.println("== ShowProductByPageServlet =2== _category " + _category);
		// 4.调用service，完成获取当前页分页Bean数据.
		ProductService service = new ProductService();
		PageBean bean = service.findProductByPage(currentPage, currentCount,
				category);
		// 将数据存储到request范围，跳转到product_list.jsp页面展示
		request.setAttribute("bean", bean);
		request.getRequestDispatcher("/client/product_list.jsp").forward(request, response);
		return;
	}
	// 将\转换为\\
	 public static String changechar(String url) {
	  String chars = url;
	  String newchar = chars.replace("/", "\\");
	  return newchar;
	 }
	 // unicode编码转换为正常汉字
	 private static String unicodeToCn(String unicode) {
	  /** 以 \ u 分割，因为java注释也能识别unicode，因此中间加了一个空格 */
	  String[] strs = unicode.split("\\\\u");
	  String returnStr = "";
	  for(int i = 1;i < strs.length; i++) {
	   returnStr += (char) Integer.valueOf(strs[i], 16).intValue();
	  }
	  return returnStr;
	 }
}